<?php
/**
 * Created by PhpStorm.
 * User: 28768
 * Date: 2020/5/21
 * Time: 2:32
 */

namespace app\admin\controller;

use Think\Db;
use think\Loader;

class Role extends Backend{
    public function index(){
        // 使用数组查询
        $name =$this->request->param('name');
        $list = Db::table('role')
            ->order('id desc')
            ->where('name','like','%'.$name.'%')
            ->where('status',1)
            ->paginate(2);
        // 模板变量赋值
        $this->assign('name',$name);
        $this->assign('list',$list);
        return $this->fetch();
    }
    public function add(){
        if ($this->request->isPost()) {
            $param = [
                'name' => 'name/s',
                'status'=>'status/s',
            ];
            $param_data = $this->buildParam($param);
            $db = Db::table('role')->insert($param_data);
            if ($db){
                return $this->success('新增用户成功！');
            }else{
                return $this->error('新增用户失败！');
            }
        }
        return $this->fetch();
    }
    public function edit(){
        $id =$this->request->param('id');
        if ($this->request->isPost()) {
            $param = [
                'name' => 'name/s',
                'status' => 'status/s',
            ];
            $param_data = $this->buildParam($param);
            $db = Db::table('role')->where('id', $id)->update($param_data);
            if ($db){
                return $this->success('编辑用户成功！');
            }else{
                return $this->error('编辑用户失败！');
            }
        }
        $role = Db::table('role')->where('id',$id)->find();
        $this->assign('role',$role);
        return $this->fetch();
    }
}